package com.etc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 用于调用省市所有推荐景点api的控制器
 */
@RestController
public class CORSController {
    @Autowired
    RestTemplate restTemplate;

    /**
     * 调用api，拼接callback，解决跨域问题
     *
     * @param callback
     * @param pid
     * @param cid
     * @return
     */
    @GetMapping("pjson")
    public String getJson(String callback,
                          @RequestParam(value = "pid") String pid,
                          @RequestParam(value = "cid") String cid,
                          ModelMap map) {
        map.addAttribute("pid", pid);
        map.addAttribute("cid", cid);
        String result = restTemplate.getForObject("http://apis.haoservice.com/lifeservice/travel/scenery?pid=" + pid + "&cid=" + cid + "&page=1&key=b4001a9c860a4f97a8d222e89d405555", String.class);
        result = callback + "(" + result + ")";
        return result;
    }
}
